import api from '@/api'
import { timeTransfer } from '@/utils/format.js'

const users = {
	state: {
		newsList:[],//消息数据列表
		roleList:[],//项目角色
		roleTree:[],//角色树
		authorList:[],//用户权限列表
		cad_suffix:"dwg,dxf,dws,dwt", //CAD文件后缀名
		cons_suffix:"png,jpg,pdf,xls,ppt,doc,xlsx,ppts,docx", //施工文件后缀名
		project:{},//项目详情
		projectId:'',//项目详情id
		device:{}, //设备详情
		task:{}, //设备详情
		productType:[ //检测设备类型
			  {
			        value: '0',
			        label: '环流监测',
			    },
			    {
			        value: '6',
			        label: '外置局放',
			    },
			    {
			        value: '8',
			        label: '地钉',
			    },
			    {
			        value: '10',
			        label: '故障报警器',
			    },
			    {
			        value: '11',
			        label: '接头感应',
			    },
		]
	},
	mutations: {
		SAVE_NEWS_NUM(state, newsList) {
			state.newsList = newsList;
		},
		SAVE_ROLE_LIST(state, roleList) {
			if(roleList.length>0){
				let list = roleList
				list.map(v=>{
					v.text = v.roleName
					v.value = v.roleId
				})
				state.roleList = list;
			}else{
				state.roleList = roleList;
			}
		},
		SAVE_ROLE_TREE(state, roleTree) {
			state.roleTree = roleTree;
		},
		SAVE_PROJECT(state, project) {
			state.project = project;
		},
		SAVE_PROJECT_ID(state, data) {
			state.projectId = data;
		},
		SAVE_AUTHOR_LIST(state, data) {
			state.authorList = data;
		},
		SAVE_DEVICE(state, data) {
			state.device = data;
		},
		SAVE_TASK(state, data) {
			state.task = data;
		},
	},
	actions: {
		save_newsList({ commit }, newsList) {
			commit('SAVE_NEWS_NUM', newsList)
		},
		save_roleList({ commit }, roleList) {
			commit('SAVE_ROLE_LIST', roleList)
		},
		save_roleTree({ commit }, roleTree) {
			commit('SAVE_ROLE_TREE', roleTree)
		},
		save_project({ commit }, project) {
			commit('SAVE_PROJECT', project)
		},
		save_project_id({ commit }, data) {
			commit('SAVE_PROJECT_ID', data)
		},
		save_author_list({ commit }, data) {
			commit('SAVE_AUTHOR_LIST', data)
		},
		save_device({ commit }, data) {
			commit('SAVE_DEVICE', data)
		},
		save_task({ commit }, data) {
			commit('SAVE_TASK', data)
		},
		/**
		 * @description: 获取消息数量
		 * @param {*} 
		 * @return {*}
		 */
		async get_newsList({ commit, dispatch }){
			let result = {}
			uni.showLoading({
				title: '加载中...'
			});
			await api.getUrgentTask().then(res=>{
				result = res;
				uni.hideLoading();
			}).catch(error=>{
				result = error;
				uni.hideLoading();
			})
			if(result.code==200 && result.data &&result.data.length>0){
				dispatch('save_newsList', result.data)
				// uni.setTabBarBadge({
				// 	index: 0,
				// 	text: result.data.length.toString()
				// })
			}else{
				dispatch('save_newsList',[]);
				
			}

		},
		/**
		 * @description: 获取所有角色
		 * @param {*} 
		 * @return {*}
		 */
		async get_roleAll({ commit, dispatch }){
			let result = {}
			// uni.showLoading({
			// 	title: '加载中...'
			// });
			await api.getRoleAll().then(res=>{
				result = res;
				uni.hideLoading();
			}).catch(err=>{
				result = error;
				uni.hideLoading();
			})
			
			if(result.code==200 && result.data &&result.data.length>0){
				dispatch('save_roleList', result.data)
			}else{
				dispatch('save_roleList',[]);
				
			}
			
		},
		/**
		 * @description: 获取角色树
		 * @param {*} 
		 * @return {*}
		 */
		async get_roleTree({ commit, dispatch }){
			let result = {}
			await api.getRoleTree({
				type:'all'
			}).then(res=>{
				result = res;
			}).catch(err=>{
				result = error;
			})
			
			if(result.code==200 && result.data &&result.data.length>0){
				dispatch('save_roleTree', result.data)
			}else{
				dispatch('save_roleTree',[]);
				
			}
			
		},
		
	}
}

export default users;


/*
[
    "system_user_add",      添加用户
    "system_user_edit",     编辑用户
    "system_user_del",      删除用户
	"system_user_query"     用户查询
    "system_user",          用户管理
    "system_project_query", 项目查询
    "system_project_add",   添加项目
    "system_project_edit",  编辑项目
    "system_project_del",   删除项目
    "system_project",       项目管理
    "system_file_add",      上传文件
	"system_file_edit"      编辑文件
	"system_file_preview"   文件预览
	"system_file_download"  文件下载
    "system_file_del",      删除文件
    "system_file",          文件管理
    "system_device_add",    添加设备
    "system_device_edit",   编辑设备
    "system_device_del",    删除设备
	"system_device_query"   设备查询
    "system_device"         设备管理 
	"system_task_add"       添加流程
	"system_task"           流程管理
]
*/